/*
* $Id: QBWnd.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBWnd.h"
#include "Components/Journal/FamilyManager.h"

CQBWnd::CQBWnd(){}
CQBWnd::~CQBWnd(){}

void CQBWnd::WindowCreate(CWnd* pParent){
	Create(AfxRegisterWndClass(CS_HREDRAW| CS_VREDRAW, LoadCursor(NULL, IDC_ARROW)),
		_T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, RECT_RIGHT_WIN, pParent, RIGHT_WIN_ID);
}

void CQBWnd::WindowDestroy(){
	DestroyWindow();
}

void CQBWnd::SetQueenBeeId(int nId){
	__super::SetQueenBeeId(nId);
	m_qb = IJournalComponent::GetQueenBeeManager()->GetQueenBee(m_nId);
	m_strNameFamily = IJournalComponent::GetFamilyManager()->GetFamily(m_qb.s_nIdFamily).m_strName;
	Invalidate();
}

void CQBWnd::Save(){}

BEGIN_MESSAGE_MAP(CQBWnd, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()

afx_msg void CQBWnd::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	CBrush brush;
	brush.CreateSolidBrush(/*GetSysColor(COLOR_3DFACE)*/RGB(255,255,255));
	dc.FillRect(rect, &brush);
	brush.DeleteObject();
	dc.SetBkMode(TRANSPARENT);
	PaintInfo(&dc, rect);
}

void CQBWnd::PaintInfo(CDC* dc, CRect rect){
	CRect rect1 = rect;
	if(!m_nId){
		CFont* pFont = CCommonFunction::GetFont(_T("Arial"), 20, TRUE, FALSE, FALSE);
		HGDIOBJ oldFont = dc->SelectObject(pFont->m_hObject);
		dc->DrawText(ILanguageManager::GetLanguageManager()->GetString(CHOOSE_QUEEN_BEE_ID).c_str(),
			rect1, DT_WORDBREAK | DT_CALCRECT);
		rect1.MoveToXY(rect.CenterPoint().x - rect1.Width()/2, rect.CenterPoint().y - rect1.Height()/2);
		dc->DrawText(ILanguageManager::GetLanguageManager()->GetString(CHOOSE_QUEEN_BEE_ID).c_str(),
			rect1, DT_CENTER | DT_WORDBREAK);
		dc->SelectObject(oldFont);
		pFont->DeleteObject();
		delete pFont;
	}else{
		CFont* pFont = CCommonFunction::GetFont(_T("Arial"), 20, TRUE, TRUE, FALSE);
		HGDIOBJ oldFont = dc->SelectObject(pFont->m_hObject);
		dc->DrawText(m_qb.s_strName.c_str(), rect1, DT_WORDBREAK | DT_CALCRECT);
		rect1.MoveToX(rect.CenterPoint().x - rect1.Width()/2);
		dc->DrawText(m_qb.s_strName.c_str(), rect1, DT_CENTER | DT_WORDBREAK);
		pFont->DeleteObject();
		delete pFont;
		int nYPos = rect1.bottom;

		rect1 = rect;
		pFont = CCommonFunction::GetFont(_T("Arial"), 18, TRUE, FALSE, FALSE);
		dc->SelectObject(pFont->m_hObject);
		std::tstring strTemp = ILanguageManager::GetLanguageManager()->GetString(
			m_qb.s_bDead ? INFO_INSPECT_DEAD_QB_ID : INFO_INSPECT_LIVE_QB_ID);
		dc->DrawText(strTemp.c_str(), rect1, DT_WORDBREAK | DT_CALCRECT);
		rect1.MoveToXY(rect.CenterPoint().x - rect1.Width()/2, nYPos);
		dc->DrawText(strTemp.c_str(), rect1, DT_CENTER | DT_WORDBREAK);
		pFont->DeleteObject();
		delete pFont;
		nYPos = rect1.bottom + 10;
		rect1 = rect;

		CFont *pFont1 = CCommonFunction::GetFont(_T("Arial"), 16, TRUE, FALSE, FALSE);
		CFont *pFont2 = CCommonFunction::GetFont(_T("Arial"), 16, FALSE, FALSE, FALSE);
		PaintPieceInfo(dc, rect, nYPos, pFont1, pFont2, INFO_INSPECT_DESCR_QB_ID, m_qb.s_strDescription);
		PaintPieceInfo(dc, rect, nYPos, pFont1, pFont2, INFO_INSPECT_FERT_QB_ID, m_qb.s_strFert);
		PaintPieceInfo(dc, rect, nYPos, pFont1, pFont2, INFO_INSPECT_BREED_QB_ID, ILanguageManager::GetLanguageManager()->GetString(BREED_HYBRID_ID + m_qb.s_nBreed));
		PaintPieceInfo(dc, rect, nYPos, pFont1, pFont2, INFO_INSPECT_DATE_QB_ID, CConvertFunction::ConvertFromDate(m_qb.s_date));
		if(m_qb.s_nIdFamily)
			PaintPieceInfo(dc, rect, nYPos, pFont1, pFont2, NAME_FAMILY_QB_ID, m_strNameFamily);
		dc->SelectObject(oldFont);
		pFont1->DeleteObject();
		pFont2->DeleteObject();
		delete pFont1;
		delete pFont2;
	}
}

void CQBWnd::PaintPieceInfo(CDC* dc, CRect rect, int& nYPos,
							CFont* pFont1, CFont* pFont2,
							int value1, const std::tstring& strValue2){
	CRect rect1 = rect;
	dc->SelectObject(pFont1->m_hObject);
	std::tstring strTemp = ILanguageManager::GetLanguageManager()->GetString(value1) + _T(":");
	dc->DrawText(strTemp.c_str(), rect1, DT_WORDBREAK | DT_CALCRECT);
	rect1.MoveToY(nYPos);rect1.right = rect.right;
	dc->DrawText(strTemp.c_str(), rect1, DT_WORDBREAK);
	nYPos = rect1.bottom;
	rect1 = rect;
	dc->SelectObject(pFont2->m_hObject);
	dc->DrawText(strValue2.c_str(), rect1, DT_WORDBREAK | DT_CALCRECT);
	rect1.MoveToY(nYPos);rect1.right = rect.right;
	dc->DrawText(strValue2.c_str(), rect1, DT_CENTER | DT_WORDBREAK);
	nYPos = rect1.bottom + 5;
}

